<?php
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 2019/3/16
 * Time: 下午10:19
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Hotel extends Model
{
    public $table = 'hotel';
    public static $error = '';
    public $primaryKey = 'hotel_id';
    protected $fillable = ['hotel_id','name','phone','concact_user','divide_rate','address','disabled'];


    public static function list($paginate=10)
    {
        $hotel = self::query()
            -> select('*')
            -> where('disabled','=',0);
        if(empty($paginate)){
            return $hotel -> get()->toArray();
        }else{
            return $hotel -> paginate($paginate);
        }
    }

    /**
     * @param $hotel_id
     * @param $post
     * @return bool
     */
    public static function save_hotel($post)
    {
        if(empty($post['hotel_id'])){
            $hotel = self::query()->create($post);
        }else{
            unset($post['_token']);
            $hotel = self::query()->where('hotel_id','=',$post['hotel_id'])->update($post);
        }
        if(empty($hotel)){
            return false;
        }
        return true;
    }

    public function api_dashbard_data($post)
    {
        $today_begin_time = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $today_end_time = $today_begin_time + 24 * 3600;
        $cmonth_begin_time = mktime(0,0,0,date('m'),1,date('Y'));
        $cmonth_end_time = mktime(23,59,59,date('m'),date('t'),date('Y'));
        $today_query = $this -> dashboard_query([
            'open_id'    => $post['hotel_open_id'],
            'begin_time' => $today_begin_time,
            'end_time'   => $today_end_time,
        ]);
        $cmonth_query = $this -> dashboard_query([
            'open_id'    => $post['hotel_open_id'],
            'begin_time' => $cmonth_begin_time,
            'end_time'   => $cmonth_end_time,
        ]);

        return [
            'current_day' => [
                'money' => $today_query -> sum('pay_fee'),
                'num'   => $today_query -> count(),
            ],
            'current_month' => [
                'money' => $cmonth_query -> sum('pay_fee'),
                'num'   => $cmonth_query -> count(),
            ],
            'online_machine_num' => 2,
            'total_machine_num'  => 2,
        ];


    }

    /**
     * 查询酒店订单公共查询方法
     */
    public function dashboard_query($condition)
    {
        $query = self::query()
            -> from('hotel_owners_apply AS hoa')
            -> join('orders','orders.hotel_id','=','hoa.hotel_id')
            -> where('order_status','=',1)
            -> where('hoa.status','=',1);//已入驻的商家
        if(!empty($condition['begin_time'])){
            $query -> where('add_time','>',$condition['begin_time']);
        }

        if(!empty($condition['end_time'])){
            $query -> where('add_time','<=',$condition['end_time']);
        }

        unset($condition['end_time'],$condition['begin_time']);

        foreach ($condition as $field => $v){
            $query -> where($field,'=',$v);
        }
        return $query;
    }


    /**
     * 酒店的删除
     */
    public static function delete_hotel($hotel_id)
    {
        try{
           $hotel_model = self::find($hotel_id);
           if(empty($hotel_model)){
             return true;
           }

           $res1 = HotelGoods::query()->where("hotel_id",'=',$hotel_id)->delete();
//           $res2 = HotelOwner::query()->where('hotel_id','=',$hotel_id)->delete();
           $res3 = HotelOwnerApply::query()->where('hotel_id','=',$hotel_id)->delete();
           $res4 = MachineModel::query()->where('hotel_id','=',$hotel_id)->delete();
           $res5 = MachineGoodsModel::query()->where('hotel_id','=',$hotel_id)->delete();

           $hotel_model -> delete();
           DB::beginTransaction();
           DB::commit();
           return true;
        }catch(\Exception $exception){
           self::$error = $exception -> getMessage();
           dd(self::$error);
           DB::rollBack();
           return false;
        }
    }









}